

#ifndef _OTA_DATA_H__
#define _OTA_DATA_H__

#include "stdint.h"
#include "OtaDataJson.h"



#define MDA_CODE_LEN_MAX 128
#define FURL_LEN_MAX 128
#define SURL_LEN_MAX 32
#define DRIVER_FILE_ADDR_ITEM_MAX 3
#define FILE_ADDR_ITEM_MAX 16
#define DEVICE_NAME_LEN_MAX 8
#define VERSION_LEN_MAX 5
#define OTA_ECU_DATA_ITEM_MAX 3
#define MSG_ID_LEN_MAX 64
#define NODE_CODE_LEN_MAX 18
#define DESCRBTION_LEN_MAX 32
#define REUQEST_LEN_MAX 32
#define OTA_ECU_FILE_NAME_MAX 8
#define OTA_DOWNLOAD_FILE_URL_MAX_LEN 256
#define MAX_BASE_CONUT 8




typedef enum{
    OTA_MSG_GET,OTA_FILE_DOWNLOADING,OTA_FILE_DOWNLOADED,OTA_FILE_CHECKING,
    OTA_DRIVE_FILE_LOADING,OTA_OTA_FILE_LOADING,OTA_PARAM_FILE_LOADING,OTA_UPGRADED
}UPGRADE_STATE;

typedef enum{
    OTA_UNKNOW,OTA_VCU,OTA_MCU,OTA_TBOX,OTA_BMS,OTA_BCM,OTA_OBC
}OTA_ECU_TYPE;

typedef enum{
    OTA_VEHICLE_UNKNOW = 0,
    OTA_AC07,
    OTA_AC08
}OTA_VehicleType;

typedef enum{
    OTA_COMPANY_UNKNOW = 0,
}OTA_ComponyType;

typedef enum{
    OTA_DEVICE_UNKNOW = 0,

}OTA_DeviceType;


typedef enum {
    OTA_UNKNOWN_MSG,OTA_CMD_MSG,OTA_PARAM_MSG
}UPGRADE_MSG_TYPE;

typedef enum {
    OTA_FILE_UNKNOW_TYPE = 0,OTA_FILE_BIN,OTA_FILE_S19,OTA_FILE_HEX,OTA_FILE_AFF
}OtaFileType;

typedef enum {
    OTA_ECU_TYPE_NONE=0,OTA_ECU_TYPE_SOLO,OTA_ECU_TYPE_DOUBLE
}OtaEcuSubType;

typedef enum {
    NONE_ID = 0,
    AC7_2101001,  //OTA_BMS,OTA_ECU_TYPE_SOLO
    AC7_2101001C,   //OTA_BMS,OTA_ECU_TYPE_DOUBLE
    AC7_2104010,   //OTA_MCU,OTA_ECU_TYPE_SOLO
    AC7_2104010B,  //OTA_MCU,OTA_ECU_TYPE_SOLO
    AC072_7913010, //OTA_TBOX,OTA_ECU_TYPE_SOLO
    AC08_7913010,  //AC8 TBOX
    AC08_2101010, //AC08 BMS
    AC08_2104010A, //AC08 MCU
    AC08_2108010, //AC08 VCU
    AC08_3735010,  //AC08 BCM
    AC08_2123030  //AC08 OBC
}OtaDeviceIdType;



typedef struct struct_OtaParam
{
    char deviceId[STR_LEN_MAX_16];
    uint8_t companyCode[STR_LEN_MAX_16];
    uint8_t targetDevice[DEVICE_NAME_LEN_MAX];
    uint8_t hardV[VERSION_LEN_MAX];
    uint8_t softV[VERSION_LEN_MAX];
}t_OtaParam;

typedef struct
{
    uint16_t linkLen; 
    uint8_t fileIndex; //此文件序号
    uint32_t fileSize; //切片文件大小
    char sunUrl[SURL_LEN_MAX];
}tOtaFileDatadAddr;

typedef struct struct_OtaEcuFileData
{
    uint32_t fileSizeSum;
    uint16_t fatherLinkLen;
    uint8_t fileName[OTA_ECU_FILE_NAME_MAX];
    OtaFileType fileType;
    uint8_t md5Code[MDA_CODE_LEN_MAX];
    uint8_t sliceFileCount;
    uint8_t fatherUrl[FURL_LEN_MAX];
    tOtaFileDatadAddr Addr[FILE_ADDR_ITEM_MAX];
}tOtaFileData;

typedef struct
{
    struct struct_OtaParam OtaParam;
    struct struct_OtaEcuFileData fileData[3];
}tOtaEcuData;

typedef struct 
{
    uint8_t forceState;
    uint8_t vehcileState;
    uint8_t chargeState;
    uint8_t gear;
    uint8_t speed;
    uint8_t minimumVolt;
    uint8_t minimumSOC;
    uint8_t powerState;
}t_OtaCondition;

typedef struct
{
    uint8_t msgId[MSG_ID_LEN_MAX];
    uint8_t nodeCode[NODE_CODE_LEN_MAX];
    uint8_t ecuCount;//目标升级个数
    uint32_t unixTime;
    t_OtaCondition OtaCondition;
    tOtaEcuData OtaEcuData[OTA_ECU_DATA_ITEM_MAX+1];  //最大支持的ECU文件升级信息数量
}t_VehicleOtaPkg;

//状态上传
typedef struct struct_otaUp
{
    uint8_t msgId[MSG_ID_LEN_MAX];
    uint8_t nodeCode[NODE_CODE_LEN_MAX];
    uint32_t unixTime;
    
}t_struct_otaUp;

typedef struct struct_EcuState
{
    uint8_t ecuName[DEVICE_NAME_LEN_MAX];
    uint8_t state;
    uint32_t descrbtion[DESCRBTION_LEN_MAX];
}t_EcuState;

typedef struct struct_OtaAnwser
{
    struct struct_otaUp otaUpData;
    uint8_t request[REUQEST_LEN_MAX];
    uint8_t state;
    uint8_t descrbtion[DESCRBTION_LEN_MAX];
}t_OtaAnwser;


typedef struct struct_OtaStateUp
{
    struct struct_otaUp otaUpData;
    uint8_t request[REUQEST_LEN_MAX];
    struct struct_EcuState EcuState[OTA_ECU_DATA_ITEM_MAX];
}t_OtaDownloadUp;

typedef struct{
    uint32_t BinHeadAddr;
    uint32_t hasBinSize;//文本文件中包含的bin数据大小
}t_InFileBInMsg;


//文件参数
typedef struct{
    t_InFileBInMsg binMsg;
    t_InFileBInMsg hexBinMsg[MAX_BASE_CONUT];
    uint32_t downloadEcuFileSizeSum; //下载文件总大小
    int8_t fileIndex;
    uint8_t countdownFiles; //文件片段剩余数量
    uint8_t fileState;  //文件状态
    uint8_t checkState;
}t_otaFileData;


//升级过程管理结构体
typedef enum{OTA_PARAM_FILE=0,OTA_DRIVER_FILE,OTA_OTA_FILE}t_OtaFileTypeList;


//文件下载请求结构体
typedef struct
{
    uint32_t targetFileLen;
    uint8_t fileIndex;
    char fileName[DEVICE_NAME_LEN_MAX];
    char fileUrl[OTA_DOWNLOAD_FILE_URL_MAX_LEN]; //文件下载连接
    uint16_t urlLen;
    uint8_t downloadState;
    uint32_t downloadedLen;
    uint16_t errorCode;
}t_otaDownloadFileParam;

typedef struct struct_OtaStateManage
{
    OTA_ECU_TYPE ecuType;  //目标升级ECU
    OtaDeviceIdType deviceId;
    uint8_t upgradeState; //升级状态
    uint8_t step_ecu_files; //当前下载的第几个文件
    uint8_t step_files; //当前下载的第几个文件片段
    t_otaFileData downloadFileState[3]; //下载完成的文件状态
    t_otaDownloadFileParam otaDownloadFileParam; //目标下载文件参数
    UPGRADE_STATE cunrrentState; //当前处在哪个升级环节
    void (*upGradeFunc)();
    tOtaEcuData OtaEcuData;
}t_OtaStateManage;

typedef struct
{   
    OTA_ECU_TYPE ecuType;
    OtaEcuSubType subType;
    OtaDeviceIdType deviceId;
    char deviceIdStr[STR_LEN_MAX_16];
    void (*upGradeFunc)();
}t_EcuDeviceIdNode;


#endif
























